<template>
    <div class="list-page">
        <div class="list-header" :class="{searching:searching}">
            <div class="avatar"></div>
            <span class="name">昵称</span>
            <img class="add" src="../../assets/image/add2.png" alt="">
        </div>
        <div class="list-content" :class="{searching:searching}">
            <div class="search">
                <div class="search-inner">
                    <input v-model="search" type="text" @compositionstart="compositionstart" @compositionend="compositionend" @focus="focusSearch" @blur="blurSearch">
                    <div class="placeholder">
                        <van-icon class="icon" name="search" />
                        <span v-show="showPlaceholder">搜索</span>
                    </div>
                </div>
                <div @click="cancelSearch" class="center">
                    <span>取消</span>
                </div>
            </div>
            <ul v-show="!searching">
                <li class="list-li" @click="router.push('chat')" v-for="item of list">
                    <div class="avatar"></div>
                    <div class="content">
                        <div class="first-row">
                            <span class="name">{{ item.name }}</span>
                            <span class="time">12:17</span>
                        </div>
                        <div class="second-row">
                            <span class="message">{{ item.message }}</span>
                        </div>
                    </div>
                </li>
            </ul>
            <ul v-show="searching">
                <li class="list-li" @click="router.push('chat')" v-for="item of searchList">
                    <div class="avatar"></div>
                    <div class="content">
                        <div class="first-row">
                            <span class="name">{{ item.name }}</span>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="list-footer" v-show="!searching">
            <div class="action">
                <img src="../../assets/image/message.png" alt="">
                <span class="message">消息</span>
            </div>
            <div class="action">
                <img src="../../assets/image/contacts.png" alt="">
                <span>联系人</span>
            </div>
            <div class="action">
                <img src="../../assets/image/trends.png" alt="">
                <span>动态</span>
            </div>
        </div>
    </div>
</template>
  
<script setup lang="ts">
import { ref,reactive,watch } from "vue"
import { useRouter } from 'vue-router'

const router = useRouter()

const searching = ref(false)
const showPlaceholder = ref(true)
const search = ref('')
const list = reactive([
    {
        name:'好友一',
        message:'我们已成功添加好友，现在可以开始聊天了'
    },
    {
        name:'好友二',
        message:'我们已成功添加好友，现在可以开始聊天了'
    }
])
let searchList = reactive([
    {
        name:'好友一',
    },
    {
        name:'好友二',
    }
])

const blurSearch = ()=>{
    showPlaceholder.value = search.value === '' ? true : false
}
const focusSearch = ()=>{
    searchList = reactive([])
    searching.value = true
}
const cancelSearch=()=>{
    searching.value = false
    search.value = ''
}
const compositionstart=()=>{
    showPlaceholder.value = false
}
const compositionend=()=>{
    showPlaceholder.value = !search.value
}
watch(search,(newVal)=>{
    showPlaceholder.value = !newVal
})
</script>
  
<style lang="scss" scoped>
.list-page{
    $headerHeight: 138px;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    .list-header{
        position: absolute;
        transition: all .3s;
        top:0;
        left: 0;
        right: 0;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        height: $headerHeight;
        background-color: #51baf1;
        color: #fff;
        padding: 0 40px 0 20px;
        font-size: 36px;
        &.searching{
            top: -$headerHeight;
        }
        .avatar{
            height: 96px;
            width: 96px;
            border-radius: 50%;
            background-color: #89c997;
        }
        .name{
            margin-left: 20px;
            line-height: 30px;
        }
        .add{
            margin-left: auto;
            width: 40px;
        }
    }
    .list-content{
        overflow: auto;
        flex: 1;
        transition: all .3s;
        background-color: #f7f6f9;
        padding: 0;
        padding-top: $headerHeight;
        &.searching{
            padding-top: 0;
            .search {
                .search-inner{
                    width: calc(100% - 88px);
                    .placeholder{
                        transform:translateX(26px);
                    }
                }
                .center{
                    right: 0;
                }
            }
        }
        .search{
            display: flex;
            align-items: center;
            background-color: #fff;
            padding:24px 20px 24px;
            font-size: 0;
            overflow: hidden;
            position: relative;
            .search-inner{
                transition: all .3s;
                flex-grow: 0;
                flex-shrink: 0;
                height: 56px;
                width: 100%;
                position: relative;
                .placeholder{
                    pointer-events: none;
                    left: 0;
                    right: 0;
                    top: -1px;
                    bottom: 0;
                    position: absolute;
                    font-size: 28px;
                    color: #8D8D93;
                    display: flex;
                    align-items: center;
                    justify-content: flex-start;
                    transform: translateX(309px);
                    transition: all .3s;
                    .icon{
                        margin-right: 8px;
                    }
                }
                input{
                    width: 100%;
                    height: 56px;
                    font-size: 28px;
                    line-height: 56px;
                    border-radius: 40px;
                    border: 0;
                    background-color: #e2e1e6;
                    padding: 0 24px 0 60px;
                    box-sizing: border-box;
                    color: #333;
                }
            }
            .center{
                position: absolute;
                transition: all 0.3s;
                box-sizing: border-box;
                font-size: 28px;
                text-wrap: nowrap;
                width: 88px;
                right: -88px;
                color: #34aded;
            }
        }
        .list-li{
            padding: 20px;
            display: flex;
            align-items: center;
            background-color: #fff;
            overflow: hidden;
            .avatar{
                flex-shrink: 0;
                height: 96px;
                width: 96px;
                border-radius: 50%;
                background-color: #89c997;
            }
            .content{
                width: calc(100% - 136px);
                margin-left: 20px;
                flex-grow: 1;
                .first-row{
                    display: flex;
                    justify-content: space-between;
                    .name{
                        font-size: 28px;
                    }
                    .time{
                        color: #C1C1CB;
                        font-size: 26px;
                    }
                }
                .second-row{
                    width: 100%;
                    margin-top: 16px;
                    font-size: 24px;
                    color: #C1C1CB;
                    overflow: hidden;
                    white-space: nowrap; 
                    text-overflow: ellipsis;
                }
            }

        }
    }
    .list-footer{
        height: 138px;
        background-color: #fff;
        display: flex;
        align-items: center;
        padding: 0 80px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        .action{
            font-size: 26px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            .message{
                color: #51baf1;
            }
            span{
                margin-top: 10px;
            }
        }
        img{
            height: 52px;
        }
    }
}
</style>
  